<?php
namespace app\admin\model;
use app\base\model\BaseModel;
/**
 * 注册会员管理
 */

class UsersModel extends BaseModel {

    //完成
    protected $_auto = array (
        array('account','htmlspecialchars',3,'function'),  //用户名
        array('email','htmlspecialchars',3,'function'),  //邮箱
        array('userpass','md5',1,'function'),  //新增时密码
        array('userpass','',2,'ignore'),  //编辑时密码
        array('status','intval',3,'function'),  //状态
        array('regtime','time',1,'function'),  //注册时间
    );
    //验证
    protected $_validate = array(
        array('account','1,20', '用户名称只能为1~20个字符', 1 ,'length',3),
        array('account', '', '已存在相同的用户名', 1, 'unique',3),
        array('email','', '已存在相同的邮箱', 1 ,'unique',3),
        array('userpass', '4,250', '请输入最少4位密码', 1, 'length',1),
    );
    /**
     * 获取备份文件列表
     * @return array 文件列表
     */
    public function loadList($where = array(), $limit = 0)
    {
        $data = $this->table("users as U")->where($where)->limit($limit)->select();
        return $data;
    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        $data = $this->create();
        if(!$data){
            return false;
        }
        if($type == 'add'){
            return $this->add();
        }
        if($type == 'edit'){
            if(empty($data['uid'])){
                return false;
            }
            if (!empty($data['userpass'])){ //密码非空，处理密码加密
                $data['userpass'] = md5($data['userpass']);
            }
            $status = $this->save($data);
            if($status === false){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 获取信息
     * @param int $userId ID
     * @return array 信息
     */
    public function getInfo($userId = 1)
    {
        $map = array();
        $map['uid'] = $userId;
        return $this->where($map)->find();
    }

    /**
     * 删除信息
     * @param int $userId ID
     * @return bool 删除状态
     */
    public function delData($userId)
    {
        $map = array();
        $map['uid'] = $userId;
        return $this->where($map)->delete();
    }

    public function upUserData($userdata){
        return $this->save($userdata);
    }

    public function getUserInfo($account,$userpass){
        $map = array(
            'account' => $account,
            'userpass' => md5($userpass)
        );
        return $this->where($map)->find();
    }
}
